home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 201-225 / disk_203 / examples / startups / startup.asm < prev    next >
Assembly Source File  |  1992-05-06  |  8KB  |  276 lines

  1. ;=============================Startup.asm==============================
  2. ;For Workbench programs, or CLI programs with or without command line para-
  3. ;meters. (Allows arguments in quotes on the CLI). Also can be modified to
  4. ;exclude argument parsing, standard IO, and C exit capability. Assembled
  5. ;with Inovatronics CAPE assembler.
  6. ;Modified from Astartup.asm by Bryce Nesbitt. Additional Mods by Jeff Glatt.
  7.  
  8.   SMALLOBJ  ;CAPE directive for PC-relative addressing replaces absolute
  9.  
  10. ;************** Included Files *******************
  11.  
  12.    INCLUDE "allsyms.i"  ;CAPE include file of all Amiga system variables
  13.  
  14. ;****************** Imported *************************
  15.  
  16.    XREF   _main           ; C/asm code entry point
  17.  
  18. ;**********************************************************
  19. ;
  20. ;   Standard Program Entry Point (if MAXARGS is not 0)
  21. ;
  22. ;   main (argc, argv)
  23. ;      int   argc;    ;Passed as 32 bits. 0=from Workbench
  24. ;      char *argv[];  ;Passed as 32 bits. If from workbench,
  25. ;                     ;then this is the WB message pointer.
  26. ;
  27.  
  28. ; The next 3 equates determine what code gets included during assembly.
  29.  
  30. MAXARGS equ 31   ;max # of CLI arguments parsed (If 0, then doesn't create
  31.                 ;the argv and argc arrays, or pass parameters to _main. It
  32.                 ;will delete the parsing code during assembly.)
  33.  
  34. Cexit   equ 1   ;If 0, then only exit point for assembly via an rts in main
  35.                 ;or a jsr exit with the return code in d0. No C exit point
  36.                 ;is supported.
  37.  
  38. standard equ 1  ;If 0, doesn't set up stdin, stdout, stderr, and errno.
  39.  
  40.     SECTION StartUpCode,CODE
  41.  
  42.     XDEF    _startup,_BUFFER,exit
  43.  
  44. _BUFFER:
  45. _startup:
  46.      move.l   d0,d6       ;dosCmdLen
  47.      move.l   a0,a2       ;dosCmdBuf
  48.      lea      initialSP,a3
  49.  
  50.    ;---Get stack address
  51.      move.l   sp,(a3)+    ; initialSP
  52.  
  53.    ;---Get Exec library base
  54.      movea.l  $00000004,a6
  55.      move.l   a6,(a3)+    ;_SysBase
  56.  
  57.    ;------ Open the DOS library:
  58.      lea      DOSName,a1
  59.      jsr      _LVOOpenLibrary(a6) ; Look ma, no error check!
  60.      move.l   d0,(a3)+            ; _DOSBase
  61.      movea.l  d0,a5
  62.  
  63.    ;------ get the address of our task
  64.      movea.l  ThisTask(a6),a4
  65.      move.l   a4,(a3)+            ;_ThisTask 
  66.  
  67.    ;------ are we running as a son of Workbench?
  68.      move.l   pr_CLI(a4),d0
  69.      beq      fromWorkbench
  70.      clr.l    (a3)+      ; returnMsg
  71.  
  72. ;=======================================================================
  73. ;====== CLI Startup Code ===============================================
  74. ;=======================================================================
  75.  
  76.   IFNE MAXARGS
  77.  
  78.    ;------ find command name:
  79. fromCLI:
  80.     movea.l  pr_CLI(a4),a0
  81.     adda.l   a0,a0      ; bcpl pointer conversion
  82.     adda.l   a0,a0
  83.     movea.l  cli_CommandName(a0),a0
  84.     adda.l   a0,a0      ; bcpl pointer conversion
  85.     adda.l   a0,a0
  86.  
  87.    ;------ create buffer and array:
  88.     lea     _argvBuffer,a4
  89.     lea     _argvArray,a1
  90.     moveq   #1,d2                   ; param counter
  91.  
  92.    ;------ fetch command name:
  93.     moveq   #0,d0
  94.     move.b  (a0)+,d0   ; size of command name
  95.     move.l  a4,(a1)+   ; ptr to command name
  96.     bra.s   DoC
  97. CMD move.b  (a0)+,(a4)+
  98. DoC Dbra    d0,CMD
  99.     clr.b   (a4)+
  100.  
  101. ;------ collect parameters:
  102.    ;------ skip control characters and leading spaces:
  103.     moveq   #MAXARGS-1,d3
  104.     moveq   #' ',d0
  105.     moveq   #'"',d4
  106. parmloopa:
  107.     move.b  (a2)+,d1
  108.     subq.l  #1,d6
  109.     ble.s   parmExit   ;last char to process
  110.     cmp.b   d0,d1
  111.     ble.s   parmloopa  ;if not $21 to $7F
  112.     movea.l a4,a0      ;save beginning of arg
  113.   ;---check for quoted string
  114.     cmp.b   d4,d1
  115.     bne.s   PR
  116.    ;---process quoted string (copy chars up to control or " char, or no
  117.    ;   more chars in the command line
  118. QU  move.b  (a2)+,d1
  119.     cmp.b   d0,d1
  120.     blt.s   nxP        ;if control char
  121.     cmp.b   d4,d1
  122.     beq.s   nxP        ;if end quote
  123.     move.b  d1,(a4)+
  124.     subq.l  #1,d6
  125.     bge.s   QU
  126.     bra.s   nxP
  127.    ;------ copy parameter up to a space or control char
  128. PR  move.b  d1,(a4)+
  129.     move.b  (a2)+,d1
  130.     subq.l  #1,d6
  131.     cmp.b   d0,d1
  132.     bgt.s   PR
  133. nxP clr.b   (a4)+      ;Null terminate
  134.     addq.w  #1,d2
  135.     move.l  a0,(a1)+
  136.     Dbra    d3,parmloopa
  137.    ;------ Clear out ends (All Done)
  138. parmExit:
  139.     clr.b   (a4)
  140.     clr.l   (a1)
  141.   ;---push passed values to _main
  142.     pea      _argvArray  ; Arg array pointer
  143.     move.l   d2,-(sp)    ; Arg count
  144.  
  145.  ENDC
  146.  
  147.  IFNE standard
  148.  
  149.    ;------ get standard input handle:
  150.     movea.l  a5,a6      ; Get _DOSBase
  151.     jsr      _LVOInput(a6)
  152.     move.l   d0,(a3)+   ; _stdin
  153.  
  154.    ;------ get standard output handle:
  155.     jsr      _LVOOutput(a6)
  156.     move.l   d0,(a3)+   ; _stdout
  157.     move.l   d0,(a3)+   ; _stderr
  158.  
  159.  ENDC
  160.  
  161.    ;------ call C main entry point
  162.     bra.s    domain
  163.  
  164. ;=======================================================================
  165. ;====== Workbench Startup Code =========================================
  166. ;=======================================================================
  167.  
  168.    ;------ we are now set up.  wait for a message from our starter
  169. fromWorkbench:
  170.     ;[a4=ThisTask]
  171.     ;[a5=_DOSBase]
  172.     lea     pr_MsgPort(a4),a0   ; our process base
  173.     jsr     _LVOWaitPort(a6)
  174.     lea     pr_MsgPort(a4),a0   ; our process base
  175.     jsr     _LVOGetMsg(a6)
  176.  
  177.    ;------ save the message so we can return it later
  178.     move.l   d0,(a3)+ ; set returnMsg. NOTE: no ReplyMsg yet!
  179.  
  180.  IFNE MAXARGS
  181.  
  182.    ;------ push argc and argv.  if argc = 0 program came from
  183.    ;------ Workbench, and argv will have the WB message address.
  184.     move.l   d0,-(SP)   ; set argv to the WB message
  185.     clr.l   -(SP)       ; set argc to 0
  186.  
  187.  ENDC
  188.  
  189.    ;------ get the first argument
  190.     ;[d0=WBMessage]
  191.     movea.l  a5,a6      ;Get _DOSBase
  192.     movea.l  d0,a2
  193.     move.l   sm_ArgList(a2),d0
  194.     beq.s    docons
  195.  
  196.    ;------ and set the current directory to the same directory
  197.     movea.l  d0,a0
  198.     move.l   wa_Lock(a0),d1
  199.     jsr      _LVOCurrentDir(a6)
  200.  
  201. docons: ;------ ignore the toolwindow argument
  202. domain:
  203.     jsr     _main
  204.     moveq   #0,d0      ; Successful return code
  205.  
  206.  IFNE Cexit
  207.  
  208.  XDEF _exit
  209.  
  210.     bra.s   exit
  211.  
  212. ;************************************************************************
  213. ;   C Program exit(returncode) Function
  214.  
  215. _exit: move.l   4(sp),d0      ; extract return code
  216.  
  217.  ENDC
  218.  
  219. exit: move.l   d0,d2         ; save return code
  220.       lea      initialSP,a3  ; set "frame pointer"
  221.       move.l   (a3)+,sp      ; restore stack pointer
  222.  
  223.    ;------ close DOS library:
  224.       movea.l  (a3)+,a6
  225.       movea.l  (a3),a1
  226.       jsr      _LVOCloseLibrary(a6)
  227.  
  228.    ;------ if we ran from CLI, skip workbench cleanup:
  229.       addq.l   #8,a3         ;skip ThisTask
  230.       move.l   (a3),d0
  231.       beq.s    exitToDOS
  232.  
  233.    ;------ return the startup message to our parent
  234.    ;------ we forbid so workbench can't UnLoadSeg() us
  235.    ;------ before we are done:
  236.       addq.b   #1,TDNestCnt(a6)
  237.       ;[d0=returnMsg]
  238.       movea.l  d0,a1
  239.       jsr      _LVOReplyMsg(a6)
  240.    ;------ this rts sends us back to DOS:
  241. exitToDOS:
  242.       move.l   d2,d0
  243.       rts
  244.  
  245.  ;******************* DATA ***********************
  246.       XDEF  _SysBase,_DOSBase,_returnMsg,_ThisTask
  247.  
  248. initialSP   dc.l 0   ; initial stack pointer
  249. _SysBase    dc.l 0   ; exec library base pointer
  250. _DOSBase    dc.l 0   ; dos library base pointer
  251. _ThisTask   dc.l 0   ; address of this task
  252. _returnMsg  dc.l 0   ; Workbench message, or zero for CLI startup
  253.  
  254.   IFNE standard
  255.  
  256.       XDEF  _errno,_stdin,_stdout,_stderr
  257. _stdin      dc.l 0   ; in handle
  258. _stdout     dc.l 0   ; out handle
  259. _stderr     dc.l 0   ; error handle
  260. _errno      dc.l 0   ; error number from OS routines
  261.  
  262.   ENDC
  263.  
  264.   IFNE MAXARGS
  265.  
  266.   XDEF _argvArray,_argvBuffer
  267.  
  268. _argvArray   ds.l MAXARGS+1 ;maximum args (parameters) on CLI line
  269. _argvBuffer  ds.b 256       ;256 characters maximum on a CLI line
  270.  
  271.   ENDC
  272.  
  273. DOSName:    dc.b 'dos.library',0
  274.  
  275.   END
  276.